package cn.whdream.juc;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 
 * @author admin
 * i++的原子性 
 * 读——改——写
 */
public class TestAtomicDemo2 {
	public static void main(String[] args) {
		Atomic demo = new Atomic();
		for (int i = 0; i < 10; i++) {
			new Thread(demo).start();
		}
	}
	
	private static class Atomic implements Runnable {
		private AtomicInteger  num = new AtomicInteger(0);//此时线程安全，对共享资源修改 1.有可见性2.有原子性;利用cas算法进行处理
		@Override
		public void run() {
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("Thread"+Thread.currentThread()+" num :"+getNum());
		}
		public int getNum() {
			return num.getAndIncrement();
		}
		
		
	}
}
